package com.dfbz.service;

import com.dfbz.dao.ArticleDao;
import com.dfbz.entity.Article;
import com.dfbz.entity.PageEntity;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class ArticleService {
    private ArticleDao articleDao=new ArticleDao();
    public PageEntity<Article> findByTitle(String title, Integer currPage) {
        PageEntity<Article> pageEntity = new PageEntity<>();
        Integer pageSize = pageEntity.getPageSize();
        //其实索引
        Integer startIndex=(currPage-1)*pageSize;
        //当前页数
        List<Article> data=articleDao.currPageData(title,startIndex,pageSize);
        //总记录数
        Integer totalCount=articleDao.countByTitle(title);
        //总页数
        Integer totalPage=totalCount%pageSize==0?totalCount/pageSize:(totalCount/pageSize)+1;
        pageEntity.setCurrPage(currPage);
        pageEntity.setTotalPage(totalPage);
        pageEntity.setTotalCount(totalCount);
        pageEntity.setData(data);
        return pageEntity;
    }

    public void add(Article article) {
        article.setBrowseCount(0L);      //默认浏览量为0
        article.setPublishDate(new Date());        //当前时间发布
        articleDao.add(article);
    }

    /**
     * 更新浏览量+1
     * @param id
     */
    public void updateBrowserCount(Long id) {
        articleDao.updateBrowserCount(id);
    }

    /**
     * 根据文章id找文章
     * @param id
     * @return
     */
    public Article findById(Long id) {
        return  articleDao.findById(id);
    }

    /**
     * 文章收藏次数
     * @param
     * @param id
     * @return
     */
    public Integer countFavoriteByArticleId(Long id) {
        return articleDao.countFavoriteByArticleId(id);
    }

    /**
     * 我是否收藏过这篇文章
     * @param loginUserId
     * @param ArticlrId
     * @return
     */
    public Boolean isFavorite(Long loginUserId, Long ArticlrId) {
        Integer count = articleDao.countByUserIdAndArticleId(loginUserId, ArticlrId);

        return count == 0 ? false : true;

    }

    public List<Map<String, Object>> findUserFavorite(Long loginUserId, Long ArticlrId) {
        return articleDao.findUserFavorite(loginUserId,ArticlrId);
    }

    public void unFoucs(Long loginUserId, Long articleID) {
        articleDao.unFoucs(loginUserId,articleID);
    }

    public void Foucs(Long loginUserId, Long articleID) {
        articleDao.Foucs(loginUserId,articleID);
    }

    public PageEntity<Article> favoritePage(Integer currPage, String title, Long id) {

        PageEntity<Article> pageModel = new PageEntity<>();
        pageModel.setCurrPage(currPage);

        // 页大小
        Integer pageSize = pageModel.getPageSize();

        // 计算起始索引
        Integer startIndex = (currPage - 1) * pageSize;

        // 当前页数据
        List<Article> data = articleDao.favoritePage(id, title, startIndex, pageSize);

        // 总记录数
        Integer totalCount = articleDao.countByFavorite(id, title);

        // 总页数
        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;

        pageModel.setCurrPage(currPage);
        pageModel.setData(data);
        pageModel.setTotalCount(totalCount);
        pageModel.setTotalPage(totalPage);

        return pageModel;
    }
}
